# -*- coding: utf-8 -*-

'''
Created on 17-10-2012

@author: LONG HOANG GIANG
'''
from CrawlerLib2 import commonlib, html2text
from Model.storymodelv3 import StoryModel
import cStringIO as StringIO
import datetime
import re
import os

class MaxReading():
    
    __url = ''
    
    def __init__(self, url):
        self.__url = url
        
    def get_detail(self, url):
        tree = commonlib.loadweb(url).build_tree()
        contentNode = tree.xpath("//div[@id='chapter']")
        html = commonlib.Etree.tostring(contentNode)
        content = html2text.html2text(html, allow_image=True)
        content = re.sub(r"\n", "<br />", content)
        return content
      
    def get_list_cat(self, url):
        tree = commonlib.loadweb(url).build_tree()
        data = []
        for item in tree.xpath("//div[@id='perex']/table[@class='table-style01']//a"):
            sname = commonlib.stringify(item)
            if sname == '': continue
            link = commonlib.normalize_str(item.get('href'))
            data.append({'name': sname, 'url': link})
        return data
    
    def process(self):
        storyModel = StoryModel(DBPATH)
        storyModel.open(True)
        books = self.get_list_cat(self.__url)
        for book in books:
            catId = storyModel.add_category(commonlib.toUpper(book['name']))
            categories = self.get_list_cat(book['url'])
            for cat in categories:
                detail = self.get_detail(cat['url']).strip()
                if detail == '': continue
                cat['name'] = commonlib.toUpper(cat['name'])
                detail = '''<strong>{0}</strong><br /><br />{1}'''.format(cat['name'], detail)
                storyModel.add_story(cat['name'], detail, catId)
        storyModel.close()
        
    def processAStory(self):
        storyModel = StoryModel(DBPATH)
        storyModel.open(True)
        for cat in self.get_list_cat(self.__url):
            detail = self.get_detail(cat['url'])
            cat['name'] = commonlib.toUpper(cat['name'])
            detail = '''<strong>{0}</strong><br /><br />{1}'''.format(cat['name'], detail)
            storyModel.add_story(cat['name'], detail, 0)
        storyModel.close()

if __name__ == '__main__':
    
    DBPATH = '/kiemhiep'
    DOWNLOAD_PATH = '/download-images/'
    if not os.path.exists(DOWNLOAD_PATH): os.makedirs(DOWNLOAD_PATH, 0777)
    
    c = MaxReading('http://maxreading.com/sach-hay/bac-sy-giai-dap-ve-suc-khoe-tinh-duc/chuong-1-35597.html')
    c.processAStory()
    
    print '> Finished at {0}'.format(datetime.datetime.now())
    os._exit(1)